package dao;

import entity.Animal;
import util.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AnimalDao {

    // 查询所有动物信息
    // 获取所有动物及其分类名称
    public List<Animal> getAllAnimals() {
        List<Animal> animals = new ArrayList<>();
        String sql = "SELECT a.*, ac.category_name FROM animal a " +
                "LEFT JOIN animal_category ac ON a.category_id = ac.category_id";
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {

            while (resultSet.next()) {
                Animal animal = new Animal();
                animal.setAnimalId(resultSet.getInt("animal_id"));
                animal.setName(resultSet.getString("name"));
                animal.setBirthdate(resultSet.getTimestamp("birthdate"));
                animal.setDescription(resultSet.getString("description"));
                animal.setCategoryId(resultSet.getInt("category_id"));
                animal.setCategoryName(resultSet.getString("category_name"));
                animals.add(animal);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return animals;
    }

    // 添加新的动物
    public boolean addAnimal(Animal animal) {
        String sql = "INSERT INTO animal (name, birthdate, description, category_id) VALUES (?, ?, ?, ?)";
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, animal.getName());
            preparedStatement.setTimestamp(2, animal.getBirthdate());
            preparedStatement.setString(3, animal.getDescription());
            preparedStatement.setInt(4, animal.getCategoryId());
            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 更新动物信息
    public boolean updateAnimal(Animal animal) {
        String sql = "UPDATE animal SET name = ?, birthdate = ?, description = ?, category_id = ? WHERE animal_id = ?";
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, animal.getName());
            preparedStatement.setTimestamp(2, animal.getBirthdate());
            preparedStatement.setString(3, animal.getDescription());
            preparedStatement.setInt(4, animal.getCategoryId());
            preparedStatement.setInt(5, animal.getAnimalId());
            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除动物信息
    public boolean deleteAnimal(int animalId) {
        String sql = "DELETE FROM animal WHERE animal_id = ?";
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, animalId);
            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 根据动物ID获取动物详情
    public Animal getAnimalById(int animalId) {
        Animal animal = null;
        String sql = "SELECT a.*, ac.category_name FROM animal a " +
                "LEFT JOIN animal_category ac ON a.category_id = ac.category_id " +
                "WHERE a.animal_id = ?";
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, animalId);

            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    animal = new Animal();
                    animal.setAnimalId(resultSet.getInt("animal_id"));
                    animal.setName(resultSet.getString("name"));
                    animal.setBirthdate(resultSet.getTimestamp("birthdate"));
                    animal.setDescription(resultSet.getString("description"));
                    animal.setCategoryId(resultSet.getInt("category_id"));
                    animal.setCategoryName(resultSet.getString("category_name"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return animal;
    }
}
